package com.dx.mes.service.process.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_bom;
import com.dx.common.table.ppt.Bis_data;
import com.dx.common.table.ppt.Bis_mdl_def;
import com.dx.common.table.ppt.Bis_pam;
import com.dx.common.table.ppt.Bis_pamId;
import com.dx.common.table.ppt.Ret_box;
import com.dx.common.table.ppt.Ret_box_xfr;
import com.dx.common.table.ppt.Ret_mask_tool;
import com.dx.common.table.ppt.Ret_mtrl;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.common.table.ppt.Ret_tool_pt;
import com.dx.common.table.ppt.Ret_tool_ptId;
import com.dx.common.table.ppt.Ret_tool_st;
import com.dx.mes.common.dao.IBisBomDao;
import com.dx.mes.common.dao.IBisDataDao;
import com.dx.mes.common.dao.IBisMdlDefDao;
import com.dx.mes.common.dao.IBisPamDao;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetBoxXfrDao;
import com.dx.mes.common.dao.IRetMaskToolDao;
import com.dx.mes.common.dao.IRetMtrlDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetToolPtDao;
import com.dx.mes.common.dao.IRetToolStDao;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.facade.process.service.IFppverifyprocService;
import com.dx.mes.facade.process.tx.Fppvrfypro.FppvrfyproI;
import com.dx.mes.facade.process.tx.Fppvrfypro.FppvrfyproO;
import com.dx.mes.facade.process.tx.Fppvrfypro.FppvrfyproOA;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fppverifyprocService")
public class FppverifyprocService implements IFppverifyprocService{
	private LogUtils logUtils;
	private static String TX_NAME = T_FPPVRFYPRO;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetBoxXfrDao retBoxXfrDao;
	@Autowired
	private IRetMaskToolDao retMaskToolDao;
	@Autowired
	private IRetToolStDao retToolStDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IRetToolPtDao retToolPtDao;
	@Autowired
	private IRetMtrlDao retMtrlDao;
	@Autowired
	private IBisBomDao bisBomDao;
	@Autowired
	private IBisPamDao bisPamDao;
	@Autowired
	private IBisDataDao bisDataDao;
	
	
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FppverifyprocService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FppverifyprocService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		Session session = retPrdInfoDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = "";

		FppvrfyproI inTrx  = new FppvrfyproI();
		FppvrfyproO outTrx = new FppvrfyproO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);
	
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FppvrfyproI.class);
			outTrx = subMainProc2(inTrx, outTrx);
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retPrdInfoDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private FppvrfyproO subMainProc2(FppvrfyproI inTrx, FppvrfyproO outTrx) {
		//TODO:unload check crr_set_code parameter
		//TODO:load cst && unload WAIT cst check mtrl
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String mtrl_prod_id = "";
		/**
		 * Get Box information
		 */
		if(inTrx.getBox_id() == null || inTrx.getBox_id().equals(_SPACE)){
			outTrx.setRtn_mesg("Box ID can't be null![" + inTrx.getBox_id() + "]");
			outTrx.setRtn_code(E_FPPVRFYPRO_ITEM_EMPTY+"");
			return outTrx;
		}
		Ret_box ret_box = retBoxDao.get(inTrx.getBox_id());
		if(ret_box == null){
			outTrx.setRtn_mesg("该卡匣["+inTrx.getBox_id()+"]不存在，请检查！");
			outTrx.setRtn_code(E_FPPVRFYPRO_TOOL_STATUS_UNMATCH +"");
			return outTrx;
		}
		/**
		 * 获取机台设备状态信息
		 */
		Ret_tool_st ret_tool_st = retToolStDao.get(inTrx.getTool_id().trim());
		if(ret_tool_st == null){
			outTrx.setRtn_mesg("该设备["+inTrx.getTool_id()+"]状态信息不存在，请检查！");
			outTrx.setRtn_code(E_RET_TOOL_ST + E_READ_NOT_FOUND +"");
			return outTrx;
		}
		if(!ret_tool_st.getTool_stat().trim().equals(TOOL_STAT_RUN) && !ret_tool_st.getTool_stat().trim().equals(TOOL_STAT_IDLE) ){
			outTrx.setRtn_mesg("该设备["+inTrx.getTool_id()+"]状态["+ret_tool_st.getTool_stat().trim()+"]不正确，请检查！");
			outTrx.setRtn_code(E_RET_TOOL_ST + E_READ_NOT_FOUND +"");
			return outTrx;
		}
		Ret_tool_ptId port_id = new Ret_tool_ptId();
		port_id.setTool_id_fk(inTrx.getTool_id());
		port_id.setPort_id(inTrx.getTool_port_id());
		/**
		 * 获取机台设备端口信息
		 */
		Ret_tool_pt ret_tool_pt = retToolPtDao.get(port_id);
		if(ret_tool_pt == null){
			outTrx.setRtn_mesg("该设备["+inTrx.getTool_id()+"]的端口号["+inTrx.getTool_port_id()+"]不存在，请检查！");
			outTrx.setRtn_code(E_RET_TOOL_PT + E_READ_NOT_FOUND +"");
			return outTrx;
		}
		
		/**
		 * Unload Port Need Check CST Setting Code && Category
		 */
		if(ret_tool_pt.getPort_typ().trim().equals(UNLOAD_PORT_TYPE)){
//			if(!ret_box.getBox_stat().equals(PROC_WAIT) && !ret_box.getBox_stat().equals(PROC_EMPTY)){
			if(!ret_box.getBox_stat().trim().equals(PROC_EMPTY)){
				outTrx.setRtn_mesg("Unload端只能上空卡匣，该卡匣["+inTrx.getBox_id()+"]的状态["+ret_box.getBox_stat()
					+"]的状态为["+ret_box.getBox_stat().trim()+"，Unload端只能上空卡匣，请检查！");
				outTrx.setRtn_code(E_FPPVRFYPRO_BOX_STATUE_ERROR +"");
				return outTrx;
			}
			rtnBeen = checkCstSettingCode(ret_box);
			if(rtnBeen.getlRc() != _NORMAL){
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return outTrx;
			}
			/**
			 * Check Unload端卡匣类型
			 */
			rtnBeen = checkCstCate4Ope(ret_box, ret_tool_st);
			if(rtnBeen.getlRc() != _NORMAL){
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return outTrx;
			}
		}else if(ret_tool_pt.getPort_typ().trim().equals(LOAD_PORT_TYPE)){
			if(!ret_box.getBox_stat().trim().equals(PROC_WAIT)){
				outTrx.setRtn_mesg("Load端只能上WAIT卡匣,该卡匣["+inTrx.getBox_id()+"]的状态["+ret_box.getBox_stat()
					+"]的状态为["+ret_box.getBox_stat()+"]，请确认！");
				outTrx.setRtn_code(E_FPPVRFYPRO_BOX_STATUE_ERROR +"");
				return outTrx;
			}
			
			//Check Material Mask && LR
			rtnBeen = checkMtrl4Tool(ret_box, ret_tool_st);
			if( rtnBeen.getObj() != null){
				mtrl_prod_id = rtnBeen.getObj().toString();
			}
			// Check WIP
			//Get Tool WIP :INPR PRD && BOX ON Port
			rtnBeen = checkWIP4Tool(ret_box, ret_tool_st, mtrl_prod_id);
			if(rtnBeen.getlRc() != _NORMAL){
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return outTrx;
			}
			//Get mdl information
			Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
			if(bis_mdl_def == null){
				outTrx.setRtn_mesg("该产品代码["+ret_box.getMdl_id_fk()+"]不存在，请检查！");
				outTrx.setRtn_code(E_BIS_MDL_DEF + E_READ_NOT_FOUND +"");
				return outTrx;
			}
			outTrx.setMdl_cate(bis_mdl_def.getMdl_cate());
			
			//Get prd information
			rtnBeen = getPrdInfo(ret_box, inTrx, outTrx);
			if(rtnBeen.getlRc() != _NORMAL){
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return outTrx;
			}
		}
		
		
		/**
		 * Load && Unload CST Need Check Material && WIP When Status == "WAIT"
		 */
//		if(ret_box.getBox_stat().trim().equals(PROC_WAIT)){
//			// Check Material Mask && LR
//			rtnBeen = checkMtrl4Tool(ret_box, ret_tool_st);
//			if( rtnBeen.getObj() != null){
//				mtrl_prod_id = rtnBeen.getObj().toString();
//			}
//			// Check WIP
//			//Get Tool WIP :INPR PRD && BOX ON Port
//			rtnBeen = checkWIP4Tool(ret_box, ret_tool_st, mtrl_prod_id);
//			if(rtnBeen.getlRc() != _NORMAL){
//				outTrx.setRtn_code(rtnBeen.getlRc() + "");
//				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
//				return outTrx;
//			}
//			//Get mdl information
//			Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
//			if(bis_mdl_def == null){
//				outTrx.setRtn_mesg("该产品代码["+ret_box.getMdl_id_fk()+"]不存在，请检查！");
//				outTrx.setRtn_code(E_BIS_MDL_DEF + E_READ_NOT_FOUND +"");
//				return outTrx;
//			}
//			outTrx.setMdl_cate(bis_mdl_def.getMdl_cate());
//			
//			//Get prd information
//			rtnBeen = getPrdInfo(ret_box, inTrx, outTrx);
//			if(rtnBeen.getlRc() != _NORMAL){
//				outTrx.setRtn_code(rtnBeen.getlRc() + "");
//				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
//				return outTrx;
//			}
//		}
		String max_slot = getBoxMaxSlot(ret_box);
		//Get Box Information
		outTrx.setBox_id(ret_box.getBox_id());
		outTrx.setBox_stat(ret_box.getBox_stat());
		outTrx.setMdl_id_fk(ret_box.getMdl_id_fk());
		outTrx.setNx_ope_id_fk(ret_box.getNx_ope_id_fk());
		outTrx.setNx_ope_no_fk(ret_box.getNx_ope_no_fk());
		outTrx.setNx_ope_ver_fk(ret_box.getNx_ope_ver_fk());
		outTrx.setNx_tool_run_mode(ret_box.getNx_tool_run_mode());
		outTrx.setMax_slot(max_slot);
		
		outTrx.setPrd_qty(ret_box.getPrd_qty().toString());
		List<Ret_prd_info> shtListInfo = retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
		if(null != shtListInfo && shtListInfo.size() >0){
			String cr_ope_id_fk = shtListInfo.get(0).getCr_ope_id_fk();
			String cr_ope_ver_fk = shtListInfo.get(0).getCr_ope_ver_fk();
			String cr_tool_id_fk = shtListInfo.get(0).getCr_tool_id_fk();
			String nx_tool_id_fk = shtListInfo.get(0).getNx_tool_id_fk();
			outTrx.setCr_ope_id_fk(cr_ope_id_fk);
			outTrx.setCr_ope_ver_fk(cr_ope_ver_fk);
			outTrx.setCr_tool_id_fk(cr_tool_id_fk);
			outTrx.setNx_tool_id_fk(nx_tool_id_fk);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
		
		return outTrx;
	}
	
	//获得最大子格位
	private String getBoxMaxSlot(Ret_box ret_box){
		String maxSlot = "";
		Bis_data bis_data = null;
		String box_set_code = ret_box.getBox_set_code();
		String hql = "FROM Bis_data where data_cate= 'BXSC' and data_ext = '"+box_set_code+"'";
		List<Bis_data> bisDataList = bisDataDao.find(hql);
		if (!bisDataList.isEmpty()) {
			bis_data = bisDataList.get(0);
		}
		maxSlot = bis_data.getExt_1();
		return maxSlot;
	}
	
	/**
	 * Check Tool WIP
	 * @param ret_box
	 * @param ret_tool_st
	 * @param mtrl_prod_id 
	 * @return
	 */
	private SimpleRtnBeen checkWIP4Tool(Ret_box ret_box, Ret_tool_st ret_tool_st, String lr_mtrl_prod_id) {
		String gszMsgBuf = "";
		//1:INPR WIP
		String hql_prd_inpr = "FROM Ret_prd_info where prd_stat='INPR' AND tool_id_fk='"
					+ret_tool_st.getTool_id_fk()+"'";
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql_prd_inpr);
		
		/**
		 * Get Load/Unload端卡匣中玻璃的信息
		 */
		List<Ret_prd_info> ret_prd_infos_load = retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
		
		
		if(ret_prd_infos != null && ret_prd_infos.size()>0){
			for(Ret_prd_info prd_info : ret_prd_infos){
				if(!prd_info.getWo_id_fk().trim().equals(ret_box.getWo_id_fk().trim())){
					gszMsgBuf ="该卡匣["+ret_box.getBox_id()+"]的工单["+ret_box.getWo_id_fk()
								+"与机台中的WIP["+prd_info.getPrd_seq_id()+"]的工单号["+prd_info.getWo_id_fk()+"]不符，请检查！"; 
					return new SimpleRtnBeen(E_FPPVRFYPRO_WIP_WO_UNMATCH, gszMsgBuf);
				}
				/**
				 * check lr_mtrl_prod_id same or not 
				 */
				if(!lr_mtrl_prod_id.isEmpty()){
					String hql = "FROM Bis_bom where mdl_id_fk='"+prd_info.getMdl_id_fk()+"' and mtrl_cate='LR'";
					List<Bis_bom> boms = bisBomDao.find(hql);
					if(boms == null || boms.size() == 0){
						gszMsgBuf ="该产品["+prd_info.getMdl_id_fk()+"]没有维护对应的光阻料号，请检查！"; 
						return new SimpleRtnBeen(E_BIS_BOM + E_READ_NOT_FOUND, gszMsgBuf);
					}
					if(!boms.get(0).getId().getMtrl_prod_id_fk().trim().equals(lr_mtrl_prod_id.trim())){
						gszMsgBuf ="该卡匣["+ret_box.getBox_id()+"]使用的光阻料号["+lr_mtrl_prod_id.trim()
							+"与机台中的WIP["+prd_info.getPrd_seq_id()+"]使用的光阻料号["+boms.get(0).getId().getMtrl_prod_id_fk().trim()
							+"]不符，请检查！"; 
						return new SimpleRtnBeen(E_FPPVRFYPRO_WIP_MTRL_UNMATCH, gszMsgBuf);
					}
				}
				
				/**
				 * check rework count
				 */
				for(int i=0;i<ret_prd_infos_load.size();i++){
					if( Integer.valueOf(ret_prd_infos_load.get(i).getPrd_pf_cnt()) != Integer.valueOf(prd_info.getPrd_pf_cnt()) ){
						gszMsgBuf = "玻璃的拔膜次数不能混！ [" + ret_prd_infos_load.get(i).getPrd_seq_id() + "]["
								+ ret_prd_infos_load.get(i).getPrd_pf_cnt() + "] vs ["
								+ prd_info.getPrd_seq_id() + "]["
								+ prd_info.getPrd_pf_cnt() + "] ";
						return new SimpleRtnBeen(SHT_PF_COUNT_MIX, gszMsgBuf);
					}
				}
			}
		}
		// WIP BOX:WAIT ON Tool Port
		String hql_box_wait = "From Ret_box_xfr where XFR_TO_TOOL_ID='"+ret_tool_st.getTool_id_fk()
					+"' and XFR_CMD_FLG='"+RTD_XFR_CMD_FLG_TON+"'";
		
		List<Ret_box_xfr> box_xfrs = retBoxXfrDao.find(hql_box_wait);
		if(box_xfrs != null && box_xfrs.size()>0){
			for(Ret_box_xfr box_xfr : box_xfrs){
				Ret_box box_wip = retBoxDao.get(box_xfr.getBox_id_fk());
				if(box_wip != null){
					if(!box_wip.getWo_id_fk().trim().equals(ret_box.getWo_id_fk())){
						gszMsgBuf ="该卡匣["+ret_box.getBox_id()+"]的工单["+ret_box.getWo_id_fk()
									+"与机台Load端卡匣["+box_wip.getBox_id()+"]的工单号["+box_wip.getWo_id_fk()+"]不符，请检查！"; 
						return new SimpleRtnBeen(E_FPPVRFYPRO_WIP_WO_UNMATCH, gszMsgBuf);
					}
					
					/**
					 * check lr_mtrl_prod_id same or not 
					 */
					String hql = "FROM Bis_bom where mdl_id_fk='"+box_wip.getMdl_id_fk()+"' and mtrl_cate='LR'";
					List<Bis_bom> boms = bisBomDao.find(hql);
					if(boms == null || boms.size() == 0){
						gszMsgBuf ="该产品["+box_wip.getMdl_id_fk()+"]没有维护对应的光阻料号，请检查！"; 
						return new SimpleRtnBeen(E_BIS_BOM + E_READ_NOT_FOUND, gszMsgBuf);
					}
					if(!box_wip.getMtrl_prod_id_fk().trim().equals(lr_mtrl_prod_id.trim())){
						gszMsgBuf ="该卡匣["+ret_box.getBox_id()+"]使用的光阻料号["+lr_mtrl_prod_id.trim()
							+"与自动派工传送中的WIP["+box_wip.getBox_id()+"]使用的光阻料号["+boms.get(0).getId().getMtrl_prod_id_fk().trim()
							+"]不符，请检查！"; 
						return new SimpleRtnBeen(E_FPPVRFYPRO_WIP_MTRL_UNMATCH, gszMsgBuf);
					}
				}
			}
		}
		return new SimpleRtnBeen(_NORMAL);
	}
	/**
	 * Check Tool Material
	 * @param ret_box
	 * @param ret_tool_st
	 * @return
	 */
	private SimpleRtnBeen checkMtrl4Tool(Ret_box ret_box, Ret_tool_st ret_tool_st) {
		SimpleRtnBeen simpleRtnBeen = new SimpleRtnBeen();
		String gszMsgBuf = "";
		String[] run_mode={"BM","R","G","B","ITO","PS","OC"};
		//黄光机台需要Mount光罩 && 光阻
		if(Arrays.asList(run_mode).contains(ret_tool_st.getTool_run_mode())){
			Ret_mask_tool ret_mask_tool = retMaskToolDao.get(ret_tool_st.getTool_id_fk().trim());
			if(ret_mask_tool == null){
				gszMsgBuf = "该设备["+ret_tool_st+"]没有装配光罩，请检查！";
				return new SimpleRtnBeen(E_RET_MASK_TOOL + E_READ_NOT_FOUND, gszMsgBuf);
			}
			//TODO:Check Mount or not && Material same or not
			String hql = "From Ret_mtrl where mtrl_location='"+ret_tool_st.getTool_id_fk()+"'";
			List<Ret_mtrl> reMtrls = retMtrlDao.find(hql);
			if(reMtrls== null || reMtrls.size()==0){
				gszMsgBuf = "该设备["+ret_tool_st+"]没有Mount光阻，请检查！";
				return new SimpleRtnBeen(E_RET_MTRL + E_READ_NOT_FOUND, gszMsgBuf);
			}
			simpleRtnBeen.setObj(reMtrls.get(0).getMtrl_prod_id_fk());
			simpleRtnBeen.setlRc(_NORMAL);
		}
		return simpleRtnBeen;
	}
	/**
	 * Check CST Setting Code
	 * @param ret_box
	 * @return
	 */
	private SimpleRtnBeen checkCstSettingCode(Ret_box ret_box){
		
		return new SimpleRtnBeen(_NORMAL);
	}
	/**
	 * Check CST Category
	 * @param ret_box
	 * @return
	 */
	private SimpleRtnBeen checkCstCate4Ope(Ret_box ret_box, Ret_tool_st ret_tool_st){
		String gszMsgBuf ="";
		//Check packing tool
		if(ret_tool_st.getTool_run_mode().trim().equals(TOOL_RUN_MODE_PACK)){
			if(!ret_box.getBox_cate().trim().equals(BOX_CATE_PPBX)){
				gszMsgBuf = "该设备["+ret_tool_st.getTool_id_fk()+"]Unload的卡匣["+ret_box.getBox_id()
						+"]类型["+ret_box.getBox_cate()+"]不符，"
						+ "应该为[PPBX],请检查！";
				return new SimpleRtnBeen(E_FPPVRFYPRO_BOX_CATE_UNMATCH, gszMsgBuf);
			}
		}
		//Check Other Tool
		if(!ret_tool_st.getTool_run_mode().trim().equals(TOOL_RUN_MODE_PACK)){
			if(!ret_box.getBox_cate().trim().equals(BOX_CATE_WCST)){
				gszMsgBuf = "该设备["+ret_tool_st.getTool_id_fk()+"]Unload的卡匣["+ret_box.getBox_id()+"]类型["
						+ret_box.getBox_cate()+"]不符，"
						+ "应该为[WCST],请检查！";
				return new SimpleRtnBeen(E_FPPVRFYPRO_BOX_CATE_UNMATCH, gszMsgBuf);
			}
		}
		return new SimpleRtnBeen(_NORMAL);
	}
	/**
	 * Get PRD inforamtion
	 * @param ret_box
	 * @param outTrx
	 * @return
	 */
	private SimpleRtnBeen getPrdInfo(Ret_box ret_box, FppvrfyproI inTrx, FppvrfyproO outTrx){
		//Get sheet information
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		String recipe_id="";
		List<FppvrfyproOA> oas = new ArrayList<FppvrfyproOA>();
		List<Ret_prd_info> prd_infos = retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
		
		
		if(prd_infos != null){
			if(prd_infos.size() != ret_box.getPrd_qty()){
				rtnBeen.setMsgBuf("卡匣的玻璃数量为["+ret_box.getPrd_qty()+"]，但是实际玻璃数量为["+prd_infos.size()+"],请检查！");
				rtnBeen.setlRc(E_FPPVRFYPRO_PRD_QTY_UNMATCH);
				return rtnBeen;
			}
			Ret_prd_info ret_prd_info_pam = prd_infos.get(0);
			/**
			 * 获取Recipe ID
			 */
			rtnBeen = getRecipeIdProc(inTrx, ret_prd_info_pam);
			if(rtnBeen.getlRc() != _NORMAL){
				return rtnBeen;
			}else{
				recipe_id = String.valueOf(rtnBeen.getObj());
			}
			outTrx.setRecipe_id(recipe_id);
			
			for(Ret_prd_info ret_prd_info : prd_infos){
				if(!ret_prd_info.getPrd_stat().equals(PROC_WAIT)){
					rtnBeen.setlRc(E_FPPVRFYPRO_BOX_STATUE_ERROR);
					rtnBeen.setMsgBuf("该玻璃["+ret_prd_info.getPrd_seq_id()+"]的状态["+ret_prd_info.getPrd_stat()
								+"]不正确，必须为["+PROC_WAIT+"]，请检查！");
					return rtnBeen;
				}
				if(!ret_prd_info.getWo_id_fk().trim().equals(ret_box.getWo_id_fk().trim())){
					rtnBeen.setlRc(E_FPPVRFYPRO_WIP_WO_UNMATCH);
					rtnBeen.setMsgBuf("该卡匣["+ret_box.getBox_id()+"]的工单["+ret_box.getWo_id_fk()
							+"与卡匣中的玻璃["+ret_prd_info.getPrd_seq_id()+"]的工单号["+ret_prd_info.getWo_id_fk()+"]不符，请检查！");
					return rtnBeen;
					
				}
				FppvrfyproOA oaryA = new FppvrfyproOA();
				oaryA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
				oaryA.setSlot_no(ret_prd_info.getSlot_no());
				oaryA.setPrd_grade(ret_prd_info.getPrd_grade());
				oaryA.setPrd_qty(ret_prd_info.getPrd_qty().toString());
				oaryA.setProc_flg(_YES);//TODO:根据不同的站点，不同情况，返回不同的值
				oaryA.setSort_flg(_YES);
				oaryA.setWo_id_fk(ret_prd_info.getWo_id_fk());
				oas.add(oaryA);
			}
			outTrx.setOary(oas);
			outTrx.setCr_ope_id_fk(ret_prd_info_pam.getCr_ope_id_fk());
			outTrx.setCr_ope_no_fk(ret_prd_info_pam.getCr_ope_no_fk());
			outTrx.setCr_ope_ver_fk(ret_prd_info_pam.getCr_ope_ver_fk());
			outTrx.setCr_proc_id_fk(ret_prd_info_pam.getCr_ope_ver_fk());
			outTrx.setCr_tool_run_mode(ret_prd_info_pam.getCr_tool_run_mode());
		}else{
			rtnBeen.setlRc(E_RET_PRD_INFO + E_READ_NOT_FOUND);
			rtnBeen.setMsgBuf("该卡匣["+ret_box.getBox_id()+"]找不到玻璃信息，请检查！");
			return rtnBeen;
		}
		return new SimpleRtnBeen(_NORMAL);
	}
	
	
	private SimpleRtnBeen getRecipeIdProc(FppvrfyproI inTrx, Ret_prd_info ret_prd_info_pam){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String recipe_id = "";
		
		List<Bis_pam> bis_pams = new ArrayList<Bis_pam>();
		List<Bis_pam> bis_pams_path = new ArrayList<Bis_pam>();
		List<Bis_pam> bis_pams_ope = new ArrayList<Bis_pam>();
		/**
		 * 根据主键读取
		 */
		Bis_pamId pam_id = new Bis_pamId();
		pam_id.setTool_id_fk(inTrx.getTool_id());
		pam_id.setMdl_id_fk(ret_prd_info_pam.getMdl_id_fk());
		pam_id.setOpe_id_fk(ret_prd_info_pam.getNx_ope_id_fk());
		pam_id.setOpe_no_fk(ret_prd_info_pam.getNx_ope_no_fk());
		pam_id.setOpe_ver_fk(ret_prd_info_pam.getNx_ope_ver_fk());
		pam_id.setPath_id_fk(ret_prd_info_pam.getNx_path_id_fk());
		pam_id.setPath_ver_fk(ret_prd_info_pam.getNx_path_ver_fk());
		Bis_pam bis_pam = bisPamDao.get(pam_id);
		if(bis_pam == null){
			/**
			 * 根据产品+工艺路线+站点读取
			 */
			String hql = "FROM Bis_pam where tool_id_fk='"+inTrx.getTool_id()+"' and mdl_id_fk='"+ret_prd_info_pam.getMdl_id_fk()
						+"' and path_id_fk='" +ret_prd_info_pam.getNx_path_id_fk()+"' and path_ver_fk='"+ret_prd_info_pam.getNx_path_ver_fk()
						+"' and ope_id_fk='"+ret_prd_info_pam.getNx_ope_id_fk()+"' and ope_ver_fk='"+ret_prd_info_pam.getNx_ope_ver_fk()+"'";
			bis_pams = bisPamDao.find(hql);
			
			if(bis_pams.isEmpty()){
				/**
				 * 根据工艺路线+站点读取
				 */
				String hql_path = "FROM Bis_pam where tool_id_fk='"+inTrx.getTool_id()
						+"' and path_id_fk='" +ret_prd_info_pam.getNx_path_id_fk()+"' and path_ver_fk='"+ret_prd_info_pam.getNx_path_ver_fk()
						+"' and ope_id_fk='"+ret_prd_info_pam.getNx_ope_id_fk()+"' and ope_ver_fk='"+ret_prd_info_pam.getNx_ope_ver_fk()+"'";
				bis_pams_path = bisPamDao.find(hql_path);
				
				if(bis_pams_path.isEmpty()){
					/**
					 * 根据工艺路线+站点读取
					 */
					String hql_ope = "FROM Bis_pam where tool_id_fk='"+inTrx.getTool_id()+"' and ope_id_fk='"+ret_prd_info_pam.getNx_ope_id_fk()+
							"' and ope_ver_fk='"+ret_prd_info_pam.getNx_ope_ver_fk()+"'";
					bis_pams_ope = bisPamDao.find(hql_ope);
					
					if(bis_pams_ope.isEmpty()){
						rtnBeen.setlRc(E_BIS_PAM + E_READ_NOT_FOUND);
						rtnBeen.setMsgBuf("该设备["+inTrx.getTool_id()+"]在该站点["+ret_prd_info_pam.getNx_ope_id_fk()+"]没有维护parameter信息");
						return rtnBeen;
					}else{
						recipe_id = bis_pams_ope.get(0).getRecipe_id();
					}
				}else{
					recipe_id = bis_pams_path.get(0).getRecipe_id();
				}
			}else{
				recipe_id = bis_pams.get(0).getRecipe_id();
			}
		}else{
			recipe_id = bis_pam.getRecipe_id();
		}
		rtnBeen.setObj(recipe_id);
		rtnBeen.setlRc(_NORMAL);
		rtnBeen.setMsgBuf(RETURN_CODE_OK);
		return rtnBeen;
	}
	
}
